package jp.ac.segakuen;


import java.util.List;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;



public class SelectListTableUtils {

	public static Key getKeyForName(String name) {
		    return KeyFactory.createKey(SelectListTable.class.getSimpleName(), "ocdate");
	}

	public static SelectListTable getByPlaceHolder(String placeholder) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		SelectListTable slt = null;
		    
		try {
		    	//String query = "select from jp.ac.segakuen.SelectListTable where displayPlaceHolder == 'examin'";
			slt = pm.getObjectById(SelectListTable.class,
		    	          KeyFactory.keyToString(getKeyForName(placeholder)));
		    } catch (JDOObjectNotFoundException e) {
		    slt = new SelectListTable("NULL",placeholder,"NULL");
		    }
		    return slt;
	}

	
	public static String insertNew(String sortOrder,String listtext, String placeholder) {
		SelectListTable slt = new SelectListTable(sortOrder,listtext,placeholder);
    	PersistenceManager pm = PMF.get().getPersistenceManager();
    	try{
    		pm.makePersistentAll(slt);
    	}finally{
    		pm.close();
    	}

		return slt.getId().toString();
	}

	public static String addAndGet(String sortOrder, String listtext,String placeholder) {
		    PersistenceManager pm = PMF.get().getPersistenceManager();

		    SelectListTable slt = null;

		    try {

		      pm.setDetachAllOnCommit(true); 
		      pm.currentTransaction().begin();
		      slt = new SelectListTable(sortOrder,listtext,placeholder);

		      pm.makePersistent(slt);
		      pm.currentTransaction().commit();
		    } finally {
		      if (pm.currentTransaction().isActive()) {
		        pm.currentTransaction().rollback();
		      }
		    }
		    return slt.getListText();
	}
		  
	@SuppressWarnings("unchecked")
	public static void reset(String placeholder) {
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    Query query = pm.newQuery(SelectListTable.class);
	    query.setFilter("displayPlaceHolder == placeholder");
	    query.declareParameters("String placeholder");
	    try {
	    	List<SelectListTable> results = (List<SelectListTable>)query.execute(placeholder);
		    if(results.iterator().hasNext()){
		    	for(SelectListTable slt:results){
		    		pm.deletePersistent(slt);
		    	}
		    }
	    } catch (JDOObjectNotFoundException e) {
		        // Object isn't there, so ignore it.
	    	return;
	  } finally {
		    	pm.close();
	  }
	}

	public static void resetbyId(String delid) {
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    try{
	    	SelectListTable slt = pm.getObjectById(SelectListTable.class,Long.valueOf(delid));
	    	pm.deletePersistent(slt);
	    } catch (JDOObjectNotFoundException e) {
		        System.out.println(e);
		        return;
	    } finally {
		    	pm.close();
	    }
	}
	
	@SuppressWarnings("unchecked")
	public static void fullreset() {
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    Query query = pm.newQuery(SelectListTable.class);

	    try {
	    	List<SelectListTable> results = (List<SelectListTable>)query.execute();
		    if(results.iterator().hasNext()){
		    	for(SelectListTable slt:results){
		    		pm.deletePersistent(slt);
		    	}
		    }
	    } catch (JDOObjectNotFoundException e) {
		        // Object isn't there, so ignore it.
	    	return;
	  } finally {
		    	pm.close();
	  }
	}
	
	@SuppressWarnings("unchecked")
	public static List<SelectListTable> getPage(String placeholder) {
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    
	    Query query = pm.newQuery(SelectListTable.class);
	    
	    query.declareParameters("String placeholder");
	    StringBuilder filter = new StringBuilder();

	    if (placeholder != null ) {
	      if (filter.length() != 0) {
	        filter.append(" && ");
	      }
	      filter.append("displayPlaceHolder == placeholder");
	    }
	    System.out.println("Filter is: " + filter.toString());
	    if (filter.length() > 0) {
	      query.setFilter(filter.toString());
	    }

	    return (List<SelectListTable>) query.execute();
	  }
}
